package com.timothy.fund.data;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import android.util.Log;

import com.sina.stock.data.SinaStockInfo;
import com.timothy.fund.bean.StockA;
import com.timothy.fund.bean.StockABase;
import com.timothy.fund.data.DateUtil;

public class AnalyzeData {
	public static final String LOG_TAG = "AnalyzeData";
	
	public static List<StockA> getStockAList(List<SinaStockInfo> ssList){
		List<StockA> stockAList = new ArrayList<StockA>();
		for(SinaStockInfo ss: ssList){
			stockAList.add(new StockA("",ss.getName(),String.valueOf(ss.getNowPrice()),"","","","",""));
		}
		return stockAList;
	}

	/**
	 * 
	 * @param saMap(Key:quote; Value:infor)
	 * @param svMap(Key:quote; Value:price)
	 * @return
	 */
	public static List<StockA> getStockAPriceList(Map<String,SinaStockInfo> saMap,Map<String,Double> svMap){
		List<StockA> stockAList = new ArrayList<StockA>();
        Set<Map.Entry<String, SinaStockInfo>> set = saMap.entrySet();
        for (Iterator<Map.Entry<String, SinaStockInfo>> it = set.iterator(); it.hasNext();) {
            Map.Entry<String, SinaStockInfo> entry = (Map.Entry<String, SinaStockInfo>) it.next();
            String quote = entry.getKey();
            
            SinaStockInfo ss = (SinaStockInfo) entry.getValue();
            Double sValue = svMap.get(quote);
            
            String name = ss.getName();
            double nowprice = ss.getNowPrice();
            double discount = 100 * (sValue - nowprice)/sValue;
            
            stockAList.add(new StockA(quote,name,NumberUtil.formatData4Deci(nowprice),NumberUtil.formatData4Deci(sValue),NumberUtil.formatData2Deci(discount)+"%","","",""));
        }
		return stockAList;
	}
	
	public static StockA getStockAbyStockInfo(StockABase sab,Double price){
		StockA sa = new StockA();
		return sa;
	}
	
	
	public static List<StockABase> parseStockABases(String[] sabs){
		List<StockABase> sabsList = new ArrayList<StockABase>();
		for(String sa: sabs){
			String[] saInfo = sa.split(sa);
			String quote = saInfo[0];
			Date startDate = DateUtil.parseDate(saInfo[1]);
			Date endDate = DateUtil.parseDate(saInfo[2]);
			String reRate = saInfo[3];
			sabsList.add(new StockABase(quote,startDate,endDate,reRate));
		}
		
		return sabsList;
	}

	//得到StockA代码列表
	public static String[] parseStockAQuotes(String[] sabs){
		String[] quotes = new String[sabs.length];
		int i=0;
		for(String sab: sabs){
			String[] sa = sab.split(",");
		    quotes[i++] = sa[0];
		}
		return quotes;
	}
	
	//得到StockA代码，价格列表
	public static Map<String,Double> getStockAValues(String[] sabs){
		Map<String,Double> sValueMap = new HashMap<String,Double>();
		for(String sab: sabs){
			String[] sa = sab.split(",");
			String sQuote = sa[0];
			String sDate = sa[1]; 
			String eDate = sa[2]; 
			String rRate = sa[3]; 
			Log.i(LOG_TAG,"sQuote="+sQuote+" sDate="+sDate+" eDate="+eDate+" rRate="+rRate);
			
			String currentDate = DateUtil.getCurrentDate();
			int passedDays = (int) DateUtil.getQuot(currentDate, sDate);
			int totalDays = (int) DateUtil.getQuot(eDate, sDate);
			
			Log.i(LOG_TAG,"passedDays="+String.valueOf(passedDays)+" totalDays="+String.valueOf(totalDays));
			
			double sValue = 1.0 + (passedDays*1.0/totalDays) * Double.valueOf(rRate);
		    sValueMap.put(sQuote, sValue);
		    
		    Log.i(LOG_TAG,"sQuote="+sQuote+" sValue="+NumberUtil.formatData4Deci(sValue));
		}
		return sValueMap;
	}
	
	//隐含收益率计算
	public double calRate1Year(double tYRate,double nYRate,double price,String endDate){
		double rate1Year = 0.0;
		return rate1Year;
	}
	

}
